home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12397 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  92 lines

  1. Path: news.cc.sunysb.edu!ghauser
  2. From: ghauser@ic.sunysb.edu (George Hauser)
  3. Newsgroups: comp.lang.c
  4. Subject: Is this a C BUG??? (A string issue)
  5. Date: 31 Mar 1996 01:39:59 GMT
  6. Organization: State University of New York at Stony Brook
  7. Message-ID: <4jknpf$9k3@abel.cc.sunysb.edu>
  8. NNTP-Posting-Host: csws5.cc.sunysb.edu
  9.  
  10.  
  11. Hi....
  12.  
  13. After hours trying to debug the program that I include in the bottom
  14. I gave up and fixed the problem by checking for a string with more than two
  15. characters, before I wrote to an external file.
  16.  
  17. I am reading the whole line in one shot using fgets(). I need to pad with 
  18. blank spaces those records that are < 194, until the string is set to 194.
  19.  
  20. Sometimes my records are already filled with 194 and sometimes not.
  21.  
  22. When I get the string of a line that contains 195 chars (data + eoln) it
  23. returns a line of 193 and then the next line returns 2 characters.... 
  24.  
  25. This is WRONG fgets should return 194 characters... but it seems like it
  26. finds a NULL on space 193 and then returns the end of line and another null.
  27.  
  28. I have spent too much time with this so now even though its ugly I simply
  29. check to see if the lenght of the string is greater than 2.
  30.  
  31. If someone sees something wrong with the code, I'd appreciate the advice.
  32. But I think this is some obscure messed up hidden character.
  33.  
  34. BTW: I'm using Borlands C++ 4.52 compiler in a Win95 pc.
  35.  
  36. Thank you.
  37.  
  38. - George
  39.  
  40. Here's the buggy code.
  41. -----------------
  42.  
  43.  
  44. #define linemax 194    /* The predifined lenght of the record */
  45. void rmgarbage(char *str)
  46. {
  47.  int counter;
  48.      counter = 0;
  49.      while(str[counter] != '\0' && counter < linemax)
  50.      {
  51.          if (str[counter] == '*')
  52.              str[counter] = ' ';
  53.          counter++;
  54.      }
  55. }
  56.  
  57. void fillrecords(char *str)
  58. {
  59.  int counter;
  60.      for (counter = strlen(str) - 1; counter < linemax; counter++)
  61.                              str[counter] = ' ';
  62. }
  63.  
  64. void main(int argc, char *argv[])
  65. {
  66.  FILE *sourcefp, *destfp;    /*Source and destination pointers */
  67.  char line[linemax + 1];    /* The lenght of the line plus NULL terminator */
  68.  long int  records;        /* Number of records that can be passed to ext */
  69.  
  70.  records = 0;
  71.  while(fgets(line,linemax,sourcefp) != NULL)
  72.  {
  73.    if (strlen(line) > 2)  /* TO CORRECT THE ERROR IN THE STRING RETURN!!!!!*/
  74.    {
  75.        if (ferror(sourcefp))
  76.        {
  77.            printf("\nError while reading source file.\n");
  78.            exit(1);
  79.        }
  80.            rmgarbage(line);            /* Remove bad chars */  
  81.        fillrecords(line);            /* Format the record line */
  82.        fputs(line,destfp);  /* Write to disk */
  83.        fputc('\n',destfp);
  84.        records++;                /* Increment our counter */
  85.    }
  86. }
  87.  
  88.  fclose(sourcefp);  /* Close source file. */
  89.  fclose(destfp);    /* Close destination file. */
  90.  printf("Total Number Of Records Processed: %d \n", records);
  91. } /* End */
  92.